package cn.tx.order.feign.fallback;

import cn.tx.order.feign.client.OrderClient;
import cn.tx.pojo.domain.Order;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 订单客户端feign接口快速失败
 * @author: zealon
 * @since: 2019/7/4
 */
@Component
public class OrderClientFallBack implements FallbackFactory<OrderClient> {

    private static final Logger logger = LoggerFactory.getLogger(OrderClientFallBack.class);

    @Override
    public OrderClient create(Throwable cause) {
        return new OrderClient() {
            @Override
            public Order getOrderById(Long id) {
                logger.error("获取订单详情ID[{}] 数量失败：{}", id, cause.getMessage());
                return new Order(-1L,"熔断","---熔断---","---熔断---","---熔断---");
            }

            @Override
            public List<Order> getListOrder(Map<String, Object> params) {
                logger.error("获取订单列表ID[{}] 数量失败：{}", params.toString(), cause.getMessage());
                List<Order> orderList = new ArrayList<>();
                Order order = new Order(-1L, "熔断", "---熔断---", "---熔断---", "---熔断---");
                orderList.add(order);
                return orderList;
            }
        };
    }
}
